// 公用连表
const { modelArticle } = require("@/models/article");
const { modelUser } = require("@/models/user");
const { modelLike } = require("@/models/like");
const { modelComment } = require("@/models/comment");
const { modelCollection } = require("@/models/collection");
const { modelCompanion, modelSignup } = require("@/models/companion");
const looKup = () => {
  // 用户表
  const model_user = {
    $lookup: {
      from: modelUser.collection.name,
      localField: "author_uid",
      foreignField: "uid",
      as: "author_data",
    },
  };
  // 点赞表
  const model_like = {
    $lookup: {
      from: modelLike.collection.name,
      localField: "_id",
      foreignField: "article_id",
      as: "likes",
    },
  };
  // 评论表
  const model_comment = {
    $lookup: {
      from: modelComment.collection.name,
      localField: "_id",
      foreignField: "article_id",
      as: "comments",
    },
  };
  // 收藏表
  const model_collect = {
    $lookup: {
      from: modelCollection.collection.name,
      localField: "_id",
      foreignField: "article_id",
      as: "collections",
    },
  };
  const project = {
    // 返给前端的瀑布流数据
    $project: {
      _id: 1,
      title: 1,
      cover_image: 1,
      address: 1,
      fileType: 1,
      "author_data.avatarUrl": 1,
      "author_data.nickname": 1,
      likes: {
        $size: "$likes",
      },
    },
  };
  return {
    model_user,
    model_like,
    model_comment,
    model_collect,
    project,
  };
};

// 四个推荐游记的公用连表
const looKupRecommend = () => {
  // 游记表
  const model_Article = {
    $lookup: {
      from: modelArticle.collection.name,
      localField: "travel_id",
      foreignField: "_id",
      as: "articleData",
    },
  };
  // 用户表
  const model_reco_user = {
    $lookup: {
      from: modelUser.collection.name,
      localField: "articleData.author_uid",
      foreignField: "uid",
      as: "userData",
    },
  };
  return {
    model_Article,
    model_reco_user,
  };
};

// 关注页面：获取用户关注的作者以及作者的游记
const looKupConcern = (_id) => {
  // 点赞表
  const model_like = {
    $lookup: {
      from: modelLike.collection.name,
      localField: _id,
      foreignField: "article_id",
      as: "likes",
    },
  };
  // 评论表
  const model_comment = {
    $lookup: {
      from: modelComment.collection.name,
      localField: _id,
      foreignField: "article_id",
      as: "comments",
    },
  };
  // 收藏表
  const model_collect = {
    $lookup: {
      from: modelCollection.collection.name,
      localField: _id,
      foreignField: "article_id",
      as: "collections",
    },
  };
  return {
    model_like,
    model_comment,
    model_collect,
  };
};

// 旅游结伴：首页筛选活动
const looKupCompanion = () => {
  const model_user = {
    //用户表
    $lookup: {
      from: modelUser.collection.name,
      localField: "uid",
      foreignField: "uid",
      as: "author_data",
    },
  };
  const model_signup = {
    //报名表
    $lookup: {
      from: modelSignup.collection.name,
      localField: "_id",
      foreignField: "signup_id",
      as: "signups",
    },
  };
  const model_companion = {
    //活动表
    $lookup: {
      from: modelCompanion.collection.name,
      localField: "signup_id",
      foreignField: "_id",
      as: "my_companion",
    },
  };

  return {
    model_signup,
    model_user,
    model_companion,
  };
};

module.exports = {
  looKup,
  looKupRecommend,
  looKupConcern,
  looKupCompanion,
};
